#include "adminloginform.h"
#include "ui_adminloginform.h"
#include "widget.h"
#include <QMainWindow>
#include <QMessageBox>
#include <QSqlError>
#include <QDebug>

adminLoginForm::adminLoginForm(Mainbar *mainbar, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::adminLoginForm)
{
    ui->setupUi(this);
    //创建页面
    m_pageAdminInfo = new pageAdminInfo(mainbar, this);  // 管理员信息界面
    m_pageUserMgmt = new pageUserMgmt(mainbar,this);             // 用户管理界面
    m_pageDeviceInfo = new pageDeviceInfo(this);         // 设备信息界面
    m_pageAlarmMgmt = new pageAlarmMgmt(this);           // 告警管理界面
    m_pageDeviceMgmt = new pageDeviceMgmt(mainbar, this);         // 设备管理界面
    m_pageDataMonitor = new pageDataMonitor(this);       // 数据监控界面
    m_pageLogMgmt = new pageLogMgmt(this);               // 日志管理界面
    m_pageDataAnalysis = new pageDataAnalysis(this);     // 数据分析界面
    m_pageAdminUpdateInfo = new pageAdminUpdateInfo(this);// 修改信息界面

    // 将页面添加到堆叠窗口
    ui->stackedWidget->addWidget(m_pageAdminInfo);
    ui->stackedWidget->addWidget(m_pageUserMgmt);
    ui->stackedWidget->addWidget(m_pageDeviceInfo);
    ui->stackedWidget->addWidget(m_pageAlarmMgmt);
    ui->stackedWidget->addWidget(m_pageDeviceMgmt);
    ui->stackedWidget->addWidget(m_pageDataMonitor);
    ui->stackedWidget->addWidget(m_pageLogMgmt);
     ui->stackedWidget->addWidget(m_pageDataAnalysis);
    ui->stackedWidget->addWidget(m_pageAdminUpdateInfo);

    // 创建按钮组来管理按钮的选中状态
    m_buttonGroup = new QButtonGroup(this);
    m_buttonGroup->addButton(ui->btnAdminInfo);
    m_buttonGroup->addButton(ui->btnUserMgmt);
    m_buttonGroup->addButton(ui->btnDeviceMgmt);
    m_buttonGroup->addButton(ui->btnDataMonitor);
    m_buttonGroup->addButton(ui->btnAlarmMgmt);
    m_buttonGroup->addButton(ui->btnLogMgmt);
    m_buttonGroup->addButton(ui->btnDataAnalysis);
    m_buttonGroup->addButton(ui->btnBackLogin);
    m_buttonGroup->setExclusive(true);

    // 默认选中第一个按钮
    ui->btnAdminInfo->setChecked(true);
    ui->stackedWidget->setCurrentIndex(PAGE_ADMIN_INFO);

    //信号连接
    connect(ui->btnAdminInfo,&QPushButton::clicked,this,[=](){
        ui->btnAdminInfo->setChecked(true);
        ui->stackedWidget->setCurrentIndex(PAGE_ADMIN_INFO);
    });
    connect(ui->btnUserMgmt,&QPushButton::clicked,this,[=](){
        ui->btnUserMgmt->setChecked(true);
        ui->stackedWidget->setCurrentIndex(PAGE_USER_MGMT);
    });
    connect(ui->btnDeviceMgmt,&QPushButton::clicked,this,[=](){
        ui->btnDeviceMgmt->setChecked(true);
        ui->stackedWidget->setCurrentIndex(PAGE_DEVICE_MGMT);
    });
    connect(ui->btnDataMonitor,&QPushButton::clicked,this,[=](){
        ui->btnDataMonitor->setChecked(true);
        ui->stackedWidget->setCurrentIndex(PAGE_DATA_MONITOR);
    });
    connect(ui->btnAlarmMgmt,&QPushButton::clicked,this,[=](){
        ui->btnAlarmMgmt->setChecked(true);
        ui->stackedWidget->setCurrentIndex(PAGE_ALARM_MGMT);
    });
    connect(ui->btnLogMgmt,&QPushButton::clicked,this,[=](){
        ui->btnLogMgmt->setChecked(true);
        ui->stackedWidget->setCurrentIndex(PAGE_LOG_MGMT);
    });
    connect(ui->btnDataAnalysis,&QPushButton::clicked,this,[=](){
        ui->btnDataAnalysis->setChecked(true);
        ui->stackedWidget->setCurrentIndex(PAGE_DATA_ANALYSIS);
    });

    // m_pageAdminInfo信号槽连接
    connect(m_pageAdminInfo, &pageAdminInfo::display, this, &adminLoginForm::onDisplayChanged);
    connect(m_pageAdminInfo, &pageAdminInfo::sendUsername, m_pageAdminUpdateInfo, &pageAdminUpdateInfo::receiveUsername);

    //连接点击btnBackLogin信号和发送display登录界面信号
    connect(ui->btnBackLogin,&QPushButton::clicked,this,[=](){
        // 重置界面状态
        ui->btnAdminInfo->setChecked(true);
        ui->stackedWidget->setCurrentIndex(PAGE_ADMIN_INFO);

        // 发送信号显示登录界面
        emit display(Widget::INDEX_MAINBAR);
    });

    // 连接 pageAdminUpdateInfo 的 display 信号到内部堆叠布局
    connect(m_pageAdminUpdateInfo, &pageAdminUpdateInfo::display, this, [=]() {
        // 重置界面状态
        ui->btnAdminInfo->setChecked(true);
        ui->stackedWidget->setCurrentIndex(PAGE_ADMIN_INFO);
        // 发送信号显示登录界面
        emit display(Widget::INDEX_MAINBAR);
    });

    // 连接 pageAdminUpdateInfo 的 sendMessage 信号到当前类的槽函数
    connect(m_pageAdminUpdateInfo, &pageAdminUpdateInfo::sendMessage, this, &adminLoginForm::onPageAdminUpdateInfoMessage);
}


adminLoginForm::~adminLoginForm()
{
    delete ui;
}

void adminLoginForm::onDisplayChanged(int index)
{
    ui->stackedWidget->setCurrentIndex(index);
}

void adminLoginForm::onPageAdminUpdateInfoMessage(const QString &message)
{
    // 转发消息
    emit sendMessage(message);
}
